function gotoadd(dom){
	var dialog = editAdminDataDialog("left,top",dom,label_add,"/adminconsole/RoomsManager/Rooms/AddRoom.jsp","addDialog",AddEventClick,loadElementFinish,null);
}

function loadElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",600);
	loadFilter(dialog);
}

function AddEventClick(dialog,id){
	waitingDialog({});
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	
	 if(dialog.find("#searchByRoomType").val() == -1){
		 return false;
	 }
	 if(dialog.find("#searchByFloor").val() == -1){
		 return false;
	 }
	 if(dialog.find("#searchByBlock").val() == -1){
		 return false;
	 }
	 var roomname = dialog.find("#roomname").val();
	 var roomtype = dialog.find("#searchByRoomType").val();
	 var floor = dialog.find("#searchByFloor").val();
	 var block = dialog.find("#searchByBlock").val();
	 var description = dialog.find("#description").val();
	 var byRoomType = $("#byRoomType").val(); 
	 var byBlock = $("#byBlock").val(); 
	 var byFloor = $("#byFloor").val(); 
	 if(id == null){
			var func = "addRoom";
		}else{
			var func = "editRoom"
		}
	 var url = "/roomService";
		var data = { func : func,
				roomId : id,
				roomname: roomname,
				roomtype: roomtype,
				floor: floor,
				block: block,
				description: description,
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer,
				byRoomType: byRoomType,
				byFloor: byFloor,
				byBlock: byBlock
			}
		simpleservice(url,data,showRoomsList);
}

function gotomultiadd(dom){
	var dialog = editAdminDataDialog("left,top",dom,label_multiadd,"/adminconsole/RoomsManager/Rooms/AddMultiRooms.jsp","MultiAddDialog",MultiAddEventClick,loadMultiAddElementFinish,null);
}

function loadMultiAddElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",650);
	loadFilter(dialog);
}

function MultiAddEventClick(dialog,id){
	waitingDialog({});
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	
	if(dialog.find("#byRoomType").val() == -1){
		 return false;
	}
	if(dialog.find("#byFloor").val() == -1){
		 return false;
	}
	if(dialog.find("#byBlock").val() == -1){
		 return false;
	}
	if(dialog.find("[name=addType]:checked").val() == "Array"){
		 var roomname = dialog.find("#roomname").val();
	}else{
		 var prefix = dialog.find("#prefix").val();
		 var rmFrom = dialog.find("#rmFrom").val();
		 var rmTo = dialog.find("#rmTo").val();
		 var roomname = "";
		 for(var i =0;i < (rmTo-rmFrom)+1;i++){
			 roomname += prefix + (parseInt(rmFrom)+i)+",";
		 }
		 roomname = roomname.substring(0,roomname.length - 1);
	}
	var roomtype = dialog.find("#searchByRoomType").val();
	var floor = dialog.find("#searchByFloor").val();
	var block = dialog.find("#searchByBlock").val();
	var description = dialog.find("description").val();
	var byRoomType = $("#byRoomType").val(); 
	var byBlock = $("#byBlock").val(); 
	var byFloor = $("#byFloor").val(); 
	var url = "/roomService";
		var data = { func : "addMultiRoom",
				roomname: roomname,
				roomtype: roomtype,
				floor: floor,
				block: block,
				description: description,
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer,
				byRoomType: byRoomType,
				byFloor: byFloor,
				byBlock: byBlock
			}
		simpleservice(url,data,showRoomsList);
}

function EditDialog(dom,id){
	var dialog = editAdminDataDialog("right",dom,label_add,"/adminconsole/RoomsManager/Rooms/AddRoom.jsp","addDialog",AddEventClick,loadEditElementFinish,id);
}

function loadEditElementFinish(dialog,id){
	cursorDialogResize(dialog,{width:600});
	EditRooms(id,dialog);
}

function showRooms(id){
	var objArray = new Array();
	if(id!=null&&id!='add'&&id!="next"&&id != "previous"){
		objArray = JSON.parse(id);
	}
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(id == null){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
		var byRoomType = $("#byRoomType").val(); 
		var byBlock = $("#byBlock").val();
		var byFloor = $("#byFloor").val();
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
		var byRoomType = $("#byRoomType").val(); 
		var byBlock = $("#byBlock").val(); 
		var byFloor = $("#byFloor").val(); 
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;	
		var byRoomType = $("#byRoomType").val(); 
		var byBlock = $("#byBlock").val(); 
		var byFloor = $("#byFloor").val(); 
	}
	var link = "/roomService";
	var data = { func: "getAllRooms",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer,
				byRoomType: byRoomType,
				byFloor: byFloor,
				byBlock: byBlock
					}
	simpleservice(link,data,showRoomsList);
}

function showRoomsList(data){
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray2);
	if(checkLogOut)
		return;
	var sumtext = "";
	var text = "";
	var pageAmount = objArray2["pageAmount"];
	var line = objArray2["lineAmount"];
	var pagePointer = objArray2["pageNumber"];
	var objArray = objArray2["data"];
	var byRoomtype = objArray2["byRoomtype"];
	var byBlock = objArray2["byBlock"];
	var byFloor = objArray2["byFloor"];
	$("#listsize").html("&nbsp;("+objArray2["dataAmount"]+")");
	
	var lineNumber = (line * pagePointer) - (line-1) ;
	
	if(objArray.length == 0){
		text = "<tr><td colspan=\"9\" style=\"border-top:1px #D6D7D5 solid;border-bottom:1px #D6D7D5 solid;\">"+nodata+"</td></tr>";
		sumtext += text;
	}else{
		for(var i = 0;i< objArray.length+1;i++){
			if(i != objArray.length ){
				if(objArray[i]['floorName'] == null){
					var floorname = "";
				}else{
					var floorname = objArray[i]['floorName'];
				}
				text = "<tr id=\""+objArray[i]['id']+"\" style=\"border: 0px;\"> "+
				"<td id=\"col_1\"> "+(lineNumber+i)+" </td>"+
				"<td style=\"text-align:left\">"+
					"<input type=\"checkbox\" id=\"id_"+objArray[i]['id']+"\"  value=\""+objArray[i]['id']+"\"> </td>"+
				"<td> "+objArray[i]['roomName']+" </td>"+
				"<td> "+objArray[i]['roomtypeName']+" </td>" +
				"<td> "+objArray[i]['blockName']+" </td>"+
				"<td> "+floorname+" </td>"+
				"<td><a href=\"#\" onclick=\"EditDialog($(this),"+objArray[i]['id']+")\">Edit</a></td> "+
				"<td><a href=\"#\" onclick=\"changeStatus("+objArray[i]['id']+")\"> <span id =status"+objArray[i]['id']+">"+objArray[i]['status']+" </a></td>"+
				"</tr> ";
				sumtext += text;
			}else{
				text = "<tr>"+
				"<td colspan=\"13\" style=\"padding:0;\">"+
				"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
				"<tbody>"+
					"<tr>"+
						"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
							"<select name=\"selectpage\" onchange=\"showRooms()\">"+
							"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
						"</td>"+
						"<td width=\"31%\" align=\"center\">"+foor_view+
							"<select name=\"maxEntries\" onchange=\"showRooms()\">"+
								"<option style=\"color:#000000\"  value=\"10\">10</option>"+
								"<option style=\"color:#000000\"  value=\"20\">20</option>"+
								"<option style=\"color:#000000\"  value=\"50\">50</option>"+
								"<option style=\"color:#000000\"  value=\"100\">100</option>"+
								"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
							"</select>"+foor_rpp+" </td>" +
							"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
							" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
							"</tbody></table></td></tr>";
				sumtext += text;
			}
	}
	}

	$("#showList tr").not($("#showList").find('.tablehead').parent()).remove();
	$("#showList").append(sumtext);
	$("#pageAmount").text(pageAmount);
	if(pagePointer+1 <= pageAmount){
		$("#next").attr("onclick","showRooms(id)");
		$("#next").html("<a href=\"#\">"+foor_next+"&gt;&gt;</a>");
	}
	if(pagePointer > 1){
		$("#previous").attr("onclick","showRooms(id)");
		$("#previous").html("<a href=\"#\">&lt;&lt;"+foor_pre+"</a>");
	}
	$("[name=maxEntries]").val(line);
	for ( var i = 0; i < pageAmount; i++) {
		$("[name=selectpage]").append(
				$("<option>").val((i+1)).html((i+1)));
		}
	$("[name=selectpage]").val(pagePointer);
	$("#byRoomType").val(byRoomtype); 
	$("#byBlock").val(byBlock); 
	$("#byFloor").val(byFloor); 
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function changeStatus(id){
	waitingDialog({});
	var url = "/roomService";
	var data = { func : "changeRoom",
			roomId : id
		}
	simpleservice(url,data,changeStatusSuccess);
}

function changeStatusSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(objArray["canchange"]){
		$("#status"+objArray["data"]["id"]).text(objArray["data"]["status"]);
	}else{
		simpleDialog("Can't Change this room , Please move all reservation out from room "+objArray["data"]["roomName"]);
	}
	
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function loadFilter(dialog){
	var url = "/roomService";
	var data = { func : "loadFilter"
		}
	simpleserviceSyn(url,data,setFilter,dialog);
}

function setFilter(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	var roomtype = objArray[0];
	var block = objArray[1];
	var floor = objArray[2];
	if(dialog == null){
		var idElRoomtype = $("#byRoomType");
		var idElBlock = $("#byBlock");
		var idElFloor = $("#byFloor");
	}else{
		var idElRoomtype = dialog.find("#searchByRoomType");
		var idElFloor = dialog.find("#searchByFloor");
		var idElBlock = dialog.find("#searchByBlock");
	}
	for ( var i = 0; i < roomtype.length; i++) {
		idElRoomtype.append(
				$("<option>").val(roomtype[i]['id']).html(roomtype[i]['shortName']));
		}
	for ( var i = 0; i < block.length; i++) {
		idElBlock.append(
				$("<option>").val(block[i]['id']).html(block[i]['title']));
		}
	for ( var i = 0; i < floor.length; i++) {
		idElFloor.append(
				$("<option>").val(floor[i]['id']).html(floor[i]['title']));
		}
}

function refresh(){
	document.location.href ="/adminconsole/RoomsManager/Rooms/Rooms.jsp";
}

function deleteRooms(){
	waitingDialog({});
	var listCheckBox = "";
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	var byRoomType = $("#byRoomType").val(); 
	var byBlock = $("#byBlock").val(); 
	var byFloor = $("#byFloor").val(); 
	if(listCheckBox == ""){
		return false;
	}else{
		var url = "/roomService";
		var data = { func : "delRoom",
				roomId : listCheckBox.substring(0,listCheckBox.length - 1),
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer,
				byRoomType: byRoomType,
				byFloor: byFloor,
				byBlock: byBlock
			}
		simpleservice(url,data,DeleteSuccess);
	}
}

function DeleteSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(objArray["data"] != null){
		showRoomsList(data);
	}else{
		var list = objArray["cantdelete"];
		var roomname = "";
		for(var i=0;i<list.length;i++){
			roomname += objArray["cantdelete"][i]+" ";
		}
		simpleDialog("Can't delete this room ,Please move all reservation out from room "+roomname);
		if($("#loadingScreen").is(":visible")) closeWaitingDialog();
	}
}

function EditRooms(id,dialog){
	var url = "/roomService";
	var data = { func : "getRoomsById",
			id : id
			}
	simpleserviceWithArgs(url,data,editSuccess,dialog);
}

function editSuccess(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	objArray2 = objArray[0];
	setFilter(JSON.stringify(objArray[1]), dialog);
	dialog.find("#roomname").val(objArray2['roomName']);
	dialog.find("#searchByRoomType").val(objArray2['roomtypeString']);
	dialog.find("#searchByBlock").val(objArray2['blockString']);
	dialog.find("#searchByFloor").val(objArray2['floorString']);
	dialog.find("#description").val(objArray2['comment']);
}

function toggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = true;
		}
	}else{
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}

function ShowByFillter(roomtype,block,floor){
	
	$("#byFloor").val(floor);
	$("#byRoomType").val(roomtype);
	$("#byBlock").val(block);
	showRooms();
}